package pl.edu.agh.agents.gui.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

import pl.edu.agh.agents.gui.GUI;
import pl.edu.agh.agents.gui.GraphFileFilter;

public class ShowBrowseDialogAction extends AbstractAction {
	private static final long serialVersionUID = 6057328894305454686L;
	private JTextField nameField;
	private GUI gui;

	public ShowBrowseDialogAction(String name, JTextField filename, GUI gui) {
		super(name);

		// text field to fill with selected name
		this.nameField = filename;
		this.gui = gui;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(gui.getDir());

		FileFilter filter = new GraphFileFilter();
		chooser.setFileFilter(filter);
		int returnVal = chooser.showOpenDialog(gui.getGraphComponent());
		if (returnVal != JFileChooser.APPROVE_OPTION) {
			return;
		}

		String name = chooser.getSelectedFile().getAbsolutePath();
		if (!name.endsWith(".graph")) {
			name = name + ".graph";
		}
		gui.setDir(chooser.getCurrentDirectory());
		nameField.setText(name);
	}
}
